package org.zanateh.wargame.game.hexagon;

import java.util.ArrayList;
import java.util.List;

import org.zanateh.wargame.game.hexagon.HexField.NotInitializedException;
import org.zanateh.wargame.game.piece.IHexagonOccupant;
import org.zanateh.wargame.game.util.Coordinate;
import org.zanateh.wargame.graphics.HexagonDrawable;

public abstract class Hexagon
{
    protected HexagonDrawable mHexagonDraw = null;

    private HexField mHexField = null;
    
    private List<IHexagonOccupant> mOccupants = new ArrayList<IHexagonOccupant>();
    
    public void setHexField( HexField pHexField ) {
    	mHexField = pHexField;
    }
    
    public HexagonDrawable getDrawable() {
    	if( mHexagonDraw == null )
    	{
        	initializeDrawable();
    	}
    	
    	return mHexagonDraw;
    }

	protected abstract void initializeDrawable();

	public boolean hasDrawable() {
		return mHexagonDraw != null;
	}
	
	public Coordinate getCoordinate() throws NotInitializedException {
		if( mHexField == null ) {
			return new Coordinate(0,0);
		}
		else {
			return this.mHexField.getFieldCoordinatesForHex(this);
		}
	}
	
	public List<IHexagonOccupant> getOccupants() {
		List<IHexagonOccupant> occupants = new ArrayList<IHexagonOccupant>(mOccupants);
		return occupants;
	}

	public void addOccupant(IHexagonOccupant occupant) {
		if(mOccupants.contains(occupant)) {
			throw new IllegalArgumentException("Cannot add occupant already contained by Hexagon.");
		}
		mOccupants.add(occupant);
	}
	
	public void removeOccupant(IHexagonOccupant occupant) {
		if(occupant == null || ! mOccupants.contains(occupant)) {
			throw new IllegalArgumentException("Cannot remove occupant: not present or null.");
		}
		
		mOccupants.remove(occupant);
	}
	
	
}

